/**
 * 退出 用于iframe页面超时调用
 */
function timeout() {
	WeKnow.submit("login",  {timeout: true});
}

/**
 * 退出登录
 */
function logout() {
	WeKnow.submit('logout');
}

var WN_MENU = (function() {
	// 递归创建菜单
	function buildOneMenu(menu, level) {
		var paddingLeftStyle = '';
		if (level > 1) { // 2级菜单后需要缩进
			if (level == 2) {
				paddingLeftStyle = 'padding-left: ' + (level - 1) * 35 + 'px';
			} else {
				paddingLeftStyle = 'padding-left: ' + ((level - 2) * 15 + 35) + 'px';
			}
		}
		
		var e = '';
		if (menu.childrens == null) { // 没有2级菜单
			e += '<li class="' + (menu.available?'':'unavailable') + '">';
				e += '<a href="javascript:void(0);" ';
					if (menu.available) {
						e += ' data-url="' + menu.url + '"';
					}
					e += ' style="' + paddingLeftStyle + '">';
					if (menu.icon2 && level == 1) {
						e += '<img class="menu_icon" src="' + menu.icon2 + '"></img>';
					}
				e += '</a>';
				e += '<label>';
					e += menu.name;
				e += '</label>';
			e += '</li>';
		}
		return e;
	}
	/**
	 * 生成水平菜单( 用户个人信息 + 菜单内容)
	 */
	function build() {
		var menus = [
		    {available: true, name: "处方违规", icon2: "img/prescription_menu.png", url: "invs_prescription_violate.do"},
		    {available: true, name: "消毒登记", icon2: "img/equipment_disinfection.png", url: "invs_instrument_disinfect.do"},
//		    {available: true, name: "病历违规", icon2: "https://wenuan-static.oss-cn-shanghai.aliyuncs.com/icon/pc/invs/statistics.png", url: ""},
			{available: true, name: "医废登记", icon2: "img/waste.png", url: "invs_waste_delivery.do"},
			{available: true, name: "检测审查", icon2: "img/sewage_detection.png", url: "invs_sewage_test_report.do"},
			{available: true, name: "综合查询", icon2: "img/integrated_query.png", url: "invs_summarize.do"},
			{available: true, name: "机构信息", icon2: "img/clinic_menu.png", url: "invs_clinic_info.do"},
			{available: true, name: "新冠疑似上报", icon2: "img/xinguanbingdu.png", url: "invs_ncov_report.do"},
			{available: true, name: "统计报表", icon2: "img/statistics.png", url: "invs_statistics_statement.do"},
			{available: true, name: "同步统计", icon2: "img/sendStatistics.png", url: "invs_exchange_statistics.do"},
			];
		
		// 菜单内容
		var e = '';
		$(menus).each(function(idx, menu) {
			e += buildOneMenu(menu, 1);
		});
		var $sideMenu = $('ul#side-menu');
		if (!$sideMenu.find('li').length) {
			$(e).prependTo($sideMenu);
		}
	}

	//设置时间
	function setDateTime(){
		var t = null;
		t = setTimeout(time, 1000); //開始运行
		function time() {
			clearTimeout(t); //清除定时器
			dt = new Date();
			var weekDay = new Array("日", "一", "二", "三", "四", "五", "六");
			var y = dt.getFullYear();
			var mt = dt.getMonth() + 1<10?'0'+(dt.getMonth()+ 1):dt.getMonth()+ 1;
			var day = dt.getDate()<10?'0'+dt.getDate():dt.getDate();
			var h = dt.getHours()<10?'0'+dt.getHours():dt.getHours(); //获取时
			var m = dt.getMinutes()<10?'0'+dt.getMinutes():dt.getMinutes(); //获取分
			var s = dt.getSeconds()<10?'0'+dt.getSeconds():dt.getSeconds(); //获取秒
			var week = dt.getDay();
			document.getElementById("curTime").innerHTML =
				"<span class='timeFont'>"+y+"/"+mt+"/"+day+"</span>" +
				"<span class='timeFont'>星期"+weekDay[week]+"</span>" +
				"<span class='timeFont'>"+h+":"+m+":"+s+"</span>"
			t = setTimeout(time, 1000); //设定定时器，循环运行
		}
	}

	return {
		build: build,
		setDateTime: setDateTime,
	}
})();

$(document).ready(function() {
	
	var $sidemenu = $('#side-menu');

	WN_MENU.setDateTime();
	
	// 生成菜单
	//WN_MENU.build();
	
	// 获取需要展示的标签
	setTimeout(
		function(){
			var initPageTitle = $('#initPageTitle').val();
			var aTableList = $sidemenu.find('a');
			var noClick = true;
			for(var i = 0;i<aTableList.length;i++){
				if(aTableList[i].dataset.url == initPageTitle){
					aTableList[i].click();
					noClick = false;
				}
			}
			//如果未执行事情则默认执行第一个
			if(noClick){
				aTableList[0].click();
			}
		}, 500
	);
	
	// 退出并返回登录页面
	$("li#logout").on("click", function() {
		WeKnow.submit($(this).data("url"));
	});
	
	// 点击顶部菜单时显示逻辑页面
	$sidemenu.on("click", 'li>a,li>label', function() {
		var ACTIVE_HORIZONTAL_MENU = 'active_menu';
		var available = false;
		var $that = $(this);
		var url = $that.data("url");
		//拼接条件 0今日 1本周 10全部 其他本周
		var today = $("#today").val();
		if(today != undefined && today != null && today != ""){
			url = url + "?today=" + today;
			$("#today").val("");
		}
		var status = $("#status").val();
		if(status != undefined && status != null && status != ""){
			url = url + (url.indexOf('?')>0?"&":"?") +"status=" + status;
			$("#status").val("");
		}
		var townId = $("#townId").val();
		if(townId != undefined && townId != null && townId != ""){
			url = url + (url.indexOf('?')>0?"&":"?") +"townId=" + townId;
			$("#townId").val("");
		}
		var otherData = $("#otherData").val();
		if(otherData != undefined && otherData != null && otherData != ""){
			url = url + (url.indexOf('?')>0?"&":"?") +"otherData=" + otherData;
			$("#otherData").val("");
		}

		// 菜单可以使用
		$('#business-area').prop("src",url);
		// active 高亮色调整
		$sidemenu.find('li').removeClass(ACTIVE_HORIZONTAL_MENU);
		$that.closest('li').addClass(ACTIVE_HORIZONTAL_MENU);
	});
	// label标签按下，等同a标签效果
	$sidemenu.on("click", 'li>label', function() {
		var $that = $(this);
		$that.closest('li').find('a').click();
	});
	
	//Loads the correct sidebar on window load,
	//collapses the sidebar on window resize.
	// Sets the min-height of #page-wrapper to window size
	function loadResize() {
		var topOffset = 60;
        var width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height-90) + "px");
        }
	}
	$(window).bind("load resize", function() {
		loadResize()
    });
	loadResize(); // IE第一次加载时 “load resize”不触发问题对应
	
	var ACTIVIE_CLASS = 'active_tab';
	// panel 过来时默认active设置
	setTimeout(
		function() {
			var $initPageUrl = $('input#initPageUrl');
			if ($initPageUrl && $initPageUrl.length && $initPageUrl.val().length)
			// 默认active 设置
			$sidemenu.find('a').each(function(idx, a) {
				var $a = $(a);
				if ($a.data('url') == $initPageUrl.val()) {
					$a.closest('li').addClass('active_tab');
					return true;
				}
			});
		}, 500
	);
	
	// 首页 → 工作台 切换
	$('.my_tab').on('click',function(){
		var $that = $(this);
		// 切换tab选中状态
		$('.my_tab').removeClass(ACTIVIE_CLASS);
		$that.addClass(ACTIVIE_CLASS);
		// 是否显示菜单
		var activeTabId = $that.prop('id');
		if ( activeTabId == 'home_page' ){
			// 跳转至首页
			WeKnow.submit('invs_home_page.do');
		} else {
			// 显示第1个menu的内容
			var firstli = $('#side-menu').find('li')[0];
			var $firstAtag = $(firstli).find('a');
			// 默认显示第一个按钮
			$firstAtag.click();
		}
	});
});